package fr.inserm.u915.organifooyi.model;

import java.lang.reflect.Constructor;

public class Field {
private String name;
private String label;
private String description;
private Class<?> clazz;
public Field(String name,String label, String description,Class<?> clazz)
	{
	if(name==null) throw new NullPointerException();
	if(clazz==null) clazz=String.class;
	this.clazz=clazz;
	this.name=name;
	this.label = (label==null?name:label);
	this.description = (description==null?label:description);
	}
public Field(String name)
	{
	this(name,name,name,String.class);
	}

public Field(String name,Class<?> clazz)
	{
	this(name,name,name,clazz);
	}

public Object cast(String s)
	{
	if(s==null) return null;
	try {
		Constructor<?> ctor=this.clazz.getConstructor(String.class);
		return ctor.newInstance(s);
		}
	catch (Exception e) {
		return null;
		}
	}

@Override
public int hashCode() {
	return name.hashCode();
	}
@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (getClass() != obj.getClass())
		return false;
	Field other = (Field) obj;
	return name.equals(other.name);
	}

public String getName()
	{
	return name;
	}

public void setLabel(String label)
	{
	this.label = label;
	}

public String getLabel() {
	return label;
}

public void setDescription(String description)	
	{
	this.description = description;
	}

public String getDescription() {
	return description;
}

@Override
public String toString()
	{
	return name;
	}
}
